//
//  ViewController.swift
//  SwiftLearnAll
//
//  Created by iosdev1 on 2017/6/19.
//  Copyright © 2017年 ygsoft. All rights reserved.
//

import UIKit

class ViewController: ZyBaseViewController ,UITableViewDelegate ,UITableViewDataSource, UITableViewDataSourcePrefetching{
    
    var arrData = [
        "0小菊花UIActivityIndicatorView",
        "1进度条UIProgressView与定时器",
        "2textViewField",
        "3textView",
        "4label",
        "5button",
        "6动画效果集锦",
        "7单线程，多线程",
        "8数据库",
        "9字符串",
        "10UIScrollView",
        "11UICollectionView",
        "12swift调用OC",
        "13OC调用swift",
        "14navigationController",
        "15本地推送",
        "16远程推送",
        "17通知",
        "18二维码",
        "19WKWebView",
        "20富文本NSMutableAttributedString",
        "21分类ExtensionViewController",
        "22NSURLSession",
        "23七大手势UITapGestureRecognizer",
        "24协议代理还有Block",
        "25原生地图",
        "26继承",
        "27运行时",
        "28UISegmentedControl",
        "29Bluetooth蓝牙中心",
        "30Bluetooth蓝牙外设",
        "31关于时间Date",
        ];
    let arrDataZimu = ["A","D","E","H","K","N","T","U","V","W","X","Y","Z"]
    
    var arrRight:NSArray = NSArray.init()
    
    var tableview : UITableView? = nil;
    
    var indexLast:NSInteger! = 0
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view, typically from a nib.
        zyUIshowMessage(message: "研究完闭包之后，封装进每一个控件中，label,button,view,imageView等等")
//        self.title = "第一个界面，tableView";
        self.view.backgroundColor = UIColor.yellow;//背景色
        self.view.backgroundColor = UIColor.init(patternImage: UIImage.init(named: "girl")!);//背景图片
        //    edgesForExtendedLayout属性设置为UIRectEdgeNone，这样布局就是从导航栏下面开始了
        self.edgesForExtendedLayout = UIRectEdge.init(rawValue: 1000)//后面的数字不知道什么情况
        
        //        转化26个字母做右边栏
        let arr:NSMutableArray = NSMutableArray.init();
        for i in 0 ..< 26{
            var str:String! = String(describing: UnicodeScalar(65 + i))
            
            let index1 = str.index(str.endIndex, offsetBy: -2)
            str = str.substring(to:index1)
            
            let index2 = str.index(str.startIndex, offsetBy: 10)
            str = str.substring(from: index2)
            arr.add(str)
        }
        arrRight = arr.mutableCopy() as! NSArray
        
        tableview = UITableView.init(frame: CGRect(x:10, y:10, width:WIDTH_SCREEN - 20, height:HEIGHT_SCREEN - 20), style: UITableViewStyle.grouped);
        self.view.addSubview(tableview!);
        tableview?.backgroundColor = UIColor.brown
        tableview?.delegate = self;
        tableview?.dataSource = self;

        self.setNavigationBack()
    }
    // MARK:代理方法分类 UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arrData.count;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellId = "cellid";
        var cell = tableview?.dequeueReusableCell(withIdentifier: cellId);
        if cell == nil {
            cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellId);
            cell?.selectionStyle = UITableViewCellSelectionStyle.none
            let initView = ZyInitView.init(frame: CGRect(x:200, y:10, width:100, height:30))
            //            let initView = ZyInitView.init()
            cell?.addSubview(initView)
        }
        cell?.textLabel?.text = arrData[indexPath.row];
        return cell!;
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return arrDataZimu.count;
    }
    
//    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
//        return true;//调用下面的方法targetIndexPathForMoveFromRowAt
//    }
//
//    func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
//        print("cell移动想上或者向下")
//        let index:IndexPath = IndexPath.init(row: 2, section: 1)
//        return index;
//    }

    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return arrRight as? [String]
    }
    
    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        zyUIshowMessage(message: title)
        print("title==\(title)  index == \(index)")
        if arrDataZimu.contains(title) {
            var indexC:NSInteger
            indexC = arrDataZimu.index(of: title)!
            indexLast = indexC
        }
        return indexLast
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        NSLog("commit editingStyle===%d", indexPath.row);
        print("commit editingStyle:===\(indexPath.row)");
        NSLog("commit editingStyle:==", "\(indexPath.row)");
        arrData.remove(at: indexPath.row)
        tableView.reloadData();
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        //        为false左划删除失效
        //        return false;
        return true;
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let collection:UITableViewRowAction = UITableViewRowAction.init(style: UITableViewRowActionStyle.default, title: "收藏") { (action, index) in
            echo(data: "\(index)")
        }
        collection.backgroundColor = UIColor.orange

        let delete = UITableViewRowAction.init(style: UITableViewRowActionStyle.destructive, title: "删除") { (action, index) in
            echo(data: "\(index)")
        }

        let add = UITableViewRowAction.init(style: UITableViewRowActionStyle.destructive, title: "添加") { (action, index) in
            echo(data: "\(index)")
        }

        let image = UITableViewRowAction.init()

        return [collection, delete, add]
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除，自己定义"
    }
    
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        print("index === \(IndexPath.init(row: 1, section: 0))")
        print("index == ")
    }
    // MARK:    UITableViewCell的Pre-Fetching预加载
    func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
        print("预加载");
    }
    
    func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
        print("取消预加载");
    }
    
    // MARK:代理方法分类    UITableViewDelegate
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40;
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50;
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headView:UIView = UIView.init()
        let lblHead:UILabel = UILabel.init(frame: CGRect(x:15, y:10, width:30, height:20))
        headView.addSubview(lblHead)
        lblHead.textColor = UIColor.yellow;
        lblHead.text = arrDataZimu[section];
        return headView;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var pvc:ZyBaseViewController? = nil
        switch indexPath.row {
        case 0:
            pvc = IndicatorViewController();
        case 1:
            pvc = ProgressViewController();
        case 2:
            pvc = TextViewFieldViewController();
        case 3:
            pvc = TextViewViewController();
        case 4:
            pvc = LabelViewController();
        case 5:
            pvc = ButtonViewController();
        case 6:
            pvc = AnimationViewController();
        case 7:
            pvc = ThreadViewController();
        case 8:
            pvc = CoreDataViewController();
        case 9:
            pvc = StringViewController();
        case 10:
            pvc = ScrollViewController();
        case 11:
            if #available(iOS 9.0, *) {
                pvc = CollectionViewController()
            } else {
                // Fallback on earlier versions
            };
        case 12:
            pvc = SwiftCallOCViewController();
        case 13:
            //        ToOCViewController要写到 swift各类控件集锦-Bridging-Header （桥接文件）
            let pvc : ToOCViewController = ToOCViewController();
            self.navigationController?.pushViewController(pvc, animated: true);
        case 14:
            pvc = ScrollViewController();
        case 15:
            pvc = ScrollViewController();
        case 16:
            pvc = ScrollViewController();
        case 17:
            pvc = ScrollViewController();
        case 18:
            pvc = QRCodeViewController();
        case 19:
            pvc = WKWebViewController();
        case 20:
            pvc = AttributedStringViewController();
        case 21:
            pvc = ExtensionViewController();
        case 22:
            pvc = NSURLSessionViewController();
        case 23:
            pvc = TapGestureViewController();
        case 24:
            pvc = ProtocolAndBlockViewController();
        case 25:
            pvc = InheritViewController();
        case 26:
            pvc = InheritViewController();
        case 27:
            pvc = InheritViewController();
        case 28:
            pvc = SegmentedViewControl();
        case 29:
            pvc = CBCentralViewController();
        case 30:
            pvc = PeripheralViewController();
        case 31:
            pvc = SegmentedViewControl();
        default:
            pvc = InheritViewController();
            break;
        }
        if indexPath.row != 13 {
            self.navigationController?.pushViewController(pvc!, animated: true)
        }
    }


    func setNavigationBack() -> Void {
        self.navigationController?.navigationBar.hy_setBackgroundView(withAlpha: 0)
//        [self.navigationController.navigationBar hy_setBackgroundViewWithAlpha:0];
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}
